﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Things.Common;
using LTP.Accounts.Bus;
namespace Things.Web.GoodsImages
{
    public partial class Modify : Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    Guid GoodsImageID = new Guid(Request.Params["id"]);
                    ShowInfo(GoodsImageID);
                }
            }
        }

        private void ShowInfo(Guid GoodsImageID)
        {
            Things.BLL.GoodsImages bll = new Things.BLL.GoodsImages();
            Things.Model.GoodsImages model = bll.GetModel(GoodsImageID);
            this.lblGoodsImageID.Text = model.GoodsImageID.ToString();
            this.txtImageOrder.Text = model.ImageOrder.ToString();
            this.Image1.ImageUrl = model.ImageURL;
        }

        public void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";

            Guid GoodsImageID = Guid.Empty;
            if (Guid.TryParse(this.lblGoodsImageID.Text, out GoodsImageID) == false)
            {
                strErr += "图片ID有误！\\n";
            }
            if (!PageValidate.IsNumber(txtImageOrder.Text))
            {
                strErr += "图片顺序格式错误！\\n";
            }
            PageUtility pu = new PageUtility();
            string imageURL = pu.UploadFile(FileUpload1);

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            int ImageOrder = int.Parse(this.txtImageOrder.Text);
            DateTime CreatedDate = DateTime.Now;

            Things.BLL.GoodsImages bll = new Things.BLL.GoodsImages();
            Things.Model.GoodsImages model = bll.GetModel(GoodsImageID);
            if (model != null)
            {
                model.GoodsImageID = GoodsImageID;
                model.ImageOrder = ImageOrder;
                model.CreatedDate = CreatedDate;
                if (string.IsNullOrEmpty(imageURL) == false)
                {
                    model.ImageURL = imageURL;
                }

                bll.Update(model);
                Things.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "list.aspx?ID=" + model.GoodsID.ToString());
            }
        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Guid GoodsImageID = Guid.Empty;
            Guid.TryParse(this.lblGoodsImageID.Text, out GoodsImageID);
            Things.BLL.GoodsImages bll = new Things.BLL.GoodsImages();
            Things.Model.GoodsImages model = bll.GetModel(GoodsImageID);
            Response.Redirect("list.aspx?ID=" + model.GoodsID.ToString());
        }
    }
}
